#include <iostream>
#include <vector>
#include<unordered_map>
using namespace std;
class Solution {
public:
     int lenLongestFibSubseq(vector<int>& arr) {
        unordered_map<int, int> indices;
        int n = arr.size();
        for (int i = 0; i < n; i++) {
            indices[arr[i]] = i;
        }
        vector<vector<int>> dp(n, vector<int>(n));
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i - 1; j >= 0 && arr[j] * 2 > arr[i]; j--) {
                int k = -1;
                if (indices.count(arr[i] - arr[j])) {
                    k = indices[arr[i] - arr[j]];
                }
                if (k >= 0) {
                    dp[j][i] = max(dp[k][j] + 1, 3);
                }
                ans = max(ans, dp[j][i]);
            }
        }
        return ans;
    }

     int lenLongestFibSubseq_2(vector<int>& arr) {
        int n = arr.size() , *p = arr.data() , res = 0;
        int dp[n][n] ; memset(dp , 0 , sizeof(dp));
        int ls;
        for(int j = 0; j < n ; ++j){
            for(int i = 0 , k = j + 1; i < j &&  k < n ;){
                ls = p[i] + p[j];
                if(ls < p[k]) ++i;
                else if(ls > p[k]) ++k;
                else {
                    res = max(res , dp[j][k] = dp[i][j] + 1);
                    ++i; ++k;
                }
            }
        }
        return res ? res + 2 : 0;
    }
};
int main()
{
   Solution s;
   vector<int> arr={1,2,3,4,5,6,7,8};
   cout<<s.lenLongestFibSubseq(arr)<<endl;
   system("pause");
   return 0;
}
